package org.gdstash.ui.table;

import org.gdstash.file.DDSLoader;
import org.gdstash.item.GDItem;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.util.GDImagePool;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class GDMassImportTableModel extends DefaultTableModel {
    private List<GDItem> data;
    public GDMassImportRow[] rows;
    private Comparator<GDMassImportRow> comparator;

    public GDMassImportTableModel(final List<GDItem> data) {
        this.adjustUI();
        this.setData(data);
    }

    public void adjustUI() {
        GDMassImportRow.updateColumnNames();
        this.setColumnIdentifiers(GDMassImportRow.columnNames);
        this.setData(this.data);
    }

    public void setData(final List<GDItem> data) {
        this.data = data;
        this.rows = null;
        if (data == null) {
            this.fireTableDataChanged();
            return;
        }
        final int size = data.size();
        this.rows = new GDMassImportRow[size];
        final GDItem.LabelInfo liEmpty = new GDItem.LabelInfo();
        liEmpty.text = "";
        int i = 0;
        GDItem.LabelInfo li = null;
        for (final GDItem item : data) {
            this.rows[i] = new GDMassImportRow();
            this.rows[i].item = item;
            this.rows[i].selected = false;
            this.rows[i].baseName = item.getBaseName();
            this.rows[i].valid = item.isValid(GDStashFrame.iniConfig.sectRestrict.affixCombi, GDStashFrame.iniConfig.sectRestrict.completionAll);
            if (this.rows[i].valid) {
                this.rows[i].validIcon = GDImagePool.iconBtnOk24;
            } else {
                this.rows[i].validIcon = GDImagePool.iconBtnCancel24;
            }
            BufferedImage img = item.getImage();
            if (img != null) {
                if (GDStashFrame.iniConfig.sectUI.graphicScale != 100) {
                    final int w = img.getWidth() * GDStashFrame.iniConfig.sectUI.graphicScale / 100;
                    final int h = img.getHeight() * GDStashFrame.iniConfig.sectUI.graphicScale / 100;
                    img = DDSLoader.getScaledImage(img, w, h);
                }
                this.rows[i].icon = new ImageIcon(img);
            }
            li = item.getCompleteNameInfo(false, false);
            if (li == null) {
                li = liEmpty;
            }
            this.rows[i].name = li;
            li = item.getRequiredLevelInfo();
            if (li == null) {
                li = liEmpty;
            }
            this.rows[i].level = li;
            li = item.getPrefixInfo();
            if (li == null) {
                li = liEmpty;
            }
            this.rows[i].prefix = li;
            li = item.getSuffixInfo();
            if (li == null) {
                li = liEmpty;
            }
            this.rows[i].suffix = li;
            li = item.getModifierInfo();
            if (li == null) {
                li = liEmpty;
            }
            this.rows[i].modifier = li;
            li = item.getComponentInfo();
            if (li == null) {
                li = liEmpty;
            }
            this.rows[i].component = li;
            li = item.getComponentBonusInfo();
            if (li == null) {
                li = liEmpty;
            }
            this.rows[i].bonus = li;
            li = item.getEnchantmentInfo();
            if (li == null) {
                li = liEmpty;
            }
            this.rows[i].enchantment = li;
            li = item.getSeedHexInfo();
            if (li == null) {
                li = liEmpty;
            }
            this.rows[i].seed = li;
            li = item.getCharNameInfo();
            if (li == null) {
                li = liEmpty;
            }
            this.rows[i].charName = li;
            this.rows[i].hardcore = item.isHardcore();
            if (this.rows[i].hardcore) {
                this.rows[i].hcIcon = GDImagePool.iconBtnOk24;
            } else {
                this.rows[i].hcIcon = GDImagePool.iconBtnCancel24;
            }
            ++i;
        }
    }

    @Override
    public int getRowCount() {
        if (this.rows == null) {
            return 0;
        }
        return this.rows.length;
    }

    @Override
    public int getColumnCount() {
        return GDMassImportRow.columnNames.length;
    }

    @Override
    public String getColumnName(final int column) {
        return GDMassImportRow.columnNames[column];
    }

    @Override
    public Object getValueAt(final int row, final int column) {
        if (this.rows == null) {
            return null;
        }
        if (row < 0) {
            return null;
        }
        if (row >= this.rows.length) {
            return null;
        }
        if (this.rows[row] == null) {
            return null;
        }
        if (column == 0) {
            return this.rows[row].selected;
        }
        if (column == 1) {
            return this.rows[row].validIcon;
        }
        if (column == 2) {
            return this.rows[row].icon;
        }
        if (column == 3) {
            return this.rows[row].name.text;
        }
        if (column == 4) {
            return this.rows[row].level.text;
        }
        if (column == 5) {
            return this.rows[row].prefix.text;
        }
        if (column == 6) {
            return this.rows[row].suffix.text;
        }
        if (column == 7) {
            return this.rows[row].modifier.text;
        }
        if (column == 8) {
            return this.rows[row].component.text;
        }
        if (column == 9) {
            return this.rows[row].bonus.text;
        }
        if (column == 10) {
            return this.rows[row].enchantment.text;
        }
        if (column == 11) {
            return this.rows[row].seed.text;
        }
        if (column == 12) {
            return this.rows[row].charName.text;
        }
        if (column == 13) {
            return this.rows[row].hcIcon;
        }
        return null;
    }

    @Override
    public boolean isCellEditable(final int row, final int column) {
        return column == 0;
    }

    @Override
    public void setValueAt(final Object value, final int row, final int column) {
        if (!this.isCellEditable(row, column)) {
            return;
        }
        if (this.rows == null) {
            return;
        }
        if (row < 0) {
            return;
        }
        if (row >= this.rows.length) {
            return;
        }
        if (this.rows[row] == null) {
            return;
        }
        if (column == 0) {
            this.rows[row].selected = (boolean) value;
        }
        if (column == 1) {
            this.rows[row].validIcon = (ImageIcon) value;
        }
        if (column == 2) {
            this.rows[row].icon = (ImageIcon) value;
        }
        if (column == 3) {
            this.rows[row].name.text = (String) value;
        }
        if (column == 4) {
            this.rows[row].level.text = (String) value;
        }
        if (column == 5) {
            this.rows[row].prefix.text = (String) value;
        }
        if (column == 6) {
            this.rows[row].suffix.text = (String) value;
        }
        if (column == 7) {
            this.rows[row].modifier.text = (String) value;
        }
        if (column == 8) {
            this.rows[row].component.text = (String) value;
        }
        if (column == 9) {
            this.rows[row].bonus.text = (String) value;
        }
        if (column == 10) {
            this.rows[row].enchantment.text = (String) value;
        }
        if (column == 11) {
            this.rows[row].seed.text = (String) value;
        }
        if (column == 12) {
            if (value == null) {
                this.rows[row].charName.text = "";
            } else {
                this.rows[row].charName.text = (String) value;
            }
        }
        if (column == 13) {
            this.rows[row].hcIcon = (ImageIcon) value;
        }
    }

    @Override
    public Class getColumnClass(final int column) {
        return GDMassImportRow.COLUMN_CLASSES[column];
    }

    public String getCellText(final int row, final int column) {
        if (this.rows == null) {
            return "";
        }
        if (row < 0) {
            return "";
        }
        if (row >= this.rows.length) {
            return "";
        }
        if (this.rows[row] == null) {
            return "";
        }
        if (column == 3) {
            return this.rows[row].name.text;
        }
        if (column == 4) {
            return this.rows[row].level.text;
        }
        if (column == 5) {
            return this.rows[row].prefix.text;
        }
        if (column == 6) {
            return this.rows[row].suffix.text;
        }
        if (column == 7) {
            return this.rows[row].modifier.text;
        }
        if (column == 8) {
            return this.rows[row].component.text;
        }
        if (column == 9) {
            return this.rows[row].bonus.text;
        }
        if (column == 10) {
            return this.rows[row].enchantment.text;
        }
        if (column == 11) {
            return this.rows[row].seed.text;
        }
        if (column == 12) {
            return this.rows[row].charName.text;
        }
        return "";
    }

    public Color getCellTextColor(final int row, final int column) {
        if (this.rows == null) {
            return Color.BLACK;
        }
        if (row < 0) {
            return Color.BLACK;
        }
        if (row >= this.rows.length) {
            return Color.BLACK;
        }
        if (this.rows[row] == null) {
            return Color.BLACK;
        }
        if (column == 3) {
            return this.rows[row].name.foreground;
        }
        if (column == 4) {
            return this.rows[row].level.foreground;
        }
        if (column == 5) {
            return this.rows[row].prefix.foreground;
        }
        if (column == 6) {
            return this.rows[row].suffix.foreground;
        }
        if (column == 7) {
            return this.rows[row].modifier.foreground;
        }
        if (column == 8) {
            return this.rows[row].component.foreground;
        }
        if (column == 9) {
            return this.rows[row].bonus.foreground;
        }
        if (column == 10) {
            return this.rows[row].enchantment.foreground;
        }
        if (column == 11) {
            return this.rows[row].seed.foreground;
        }
        if (column == 12) {
            return this.rows[row].charName.foreground;
        }
        return Color.BLACK;
    }

    public void setSelectAll(final boolean selected) {
        if (this.rows == null) {
            return;
        }
        for (int i = 0; i < this.rows.length; ++i) {
            this.rows[i].selected = selected;
        }
        this.fireTableDataChanged();
    }

    public void selectValidOnly() {
        if (this.rows == null) {
            return;
        }
        for (int i = 0; i < this.rows.length; ++i) {
            this.rows[i].selected = this.rows[i].valid;
        }
        this.fireTableDataChanged();
    }

    public List<GDItem> getSelectedItems() {
        final List<GDItem> list = new LinkedList<GDItem>();
        if (this.rows == null) {
            return list;
        }
        for (int i = 0; i < this.rows.length; ++i) {
            if (this.rows[i].selected) {
                list.add(this.rows[i].item);
            }
        }
        return list;
    }

    public void sort(final Comparator<GDMassImportRow> comp) {
        if (comp == null) {
            return;
        }
        this.comparator = comp;
        if (this.rows == null) {
            return;
        }
        Arrays.<GDMassImportRow>sort(this.rows, this.comparator);
    }
}
